<?php


class metacriticGameSearch extends metacritic
{
	
	function __construct(){
		
	}
	
	public function getPage(){
		$finalUrl = str_replace ('[[probe]]',$this->_probe,$this->_baseUrl);
		$finalUrl = str_replace ('[[platforms]]','plats['.$this->_platforms[$this->_plat].']',$finalUrl);
		$content = self::__urlGetContent($finalUrl);
		/*
			echo $sFile;
	
		$content = file_get_contents ($finalUrl);
		*/
		//$content = file_get_contents('./test/scheda_page.html');
		$this->_content = $content;
		//file_put_contents ('./test/scheda_page.html',$content);
	}
	
	public function parseCode(){
		preg_match_all('/\<li class=\"result(.*?)\<\/li\>/is',$this->_content,$sheets);
		foreach ($sheets[0] as $singleGame){			
			preg_match_all ('/product_title (.*?)\<a (.*?)>(.*?)\<\/a\>/is',$singleGame,$titles);
			preg_match_all('/metascore (.*?)\"\>([0-9]+)\<\/span\>/',$singleGame,$votes);
			$vote = (!isset($votes[2][0])) ? 'tbn' : $votes[2][0];
			$this->_finalData[$this->_plat][] =  array ('title'=>trim($titles[3][0]),
														'vote'=>$vote,
														'link'=>'',
														'platform'=>self::__getMyPlatform($titles[0][0]));
		}
	}
	
	public function renderXML(){
		$xml = '<?xml version="1.0" encoding="UTF-8"?>
		<results>
		[[items]]
		</results>';
	
		$template = file_get_contents ('./template/xmlItem.xml');
		$code = "";
		foreach ($this->_finalData as $plat=>$games){
			foreach ($games as $info){
				//if (!is_numeric($info['vote'])) continue;
	
				$code .= str_replace ('[[title]]',$info['title'],$template);
				$link = str_replace ('href="','',trim($info['link']));
				$link = str_replace ('"','',$link);
	
				$code = str_replace ('[[link]]',$this->sheetUrl.$link,$code);
				$coverUrl = self::__retrieveCover($this->sheetUrl.$link);
	
				$code = str_replace ('[[cover]]',$coverUrl,$code);
				$code = str_replace ('[[vote]]',$info['vote'],$code);
				$code = str_replace ('[[platform]]',$info['platform'],$code);
	
			}
		}
		$this->_response = str_replace ('[[items]]',$code,$xml);
		return $this->_response;
	}
	
	public function renderResult(){
		$template = file_get_contents ('./template/item.html');
		$code = "";
		if (count($this->_finalData) == 0)
			return file_get_contents ('./template/no-item.html');
			
		foreach ($this->_finalData as $plat=>$games){
			foreach ($games as $info){
				if (!is_numeric($info['vote'])) continue;
				$code .= str_replace ('[[title]]',$info['title'],$template);
				$link = str_replace ('href="','',trim($info['link']));
				$link = str_replace ('"','',$link);
	
				$code = str_replace ('[[link]]',$this->sheetUrl.$link,$code);
				$coverUrl = self::__retrieveCover($this->sheetUrl.$link);
	
				$code = str_replace ('[[coverUrl]]',$coverUrl,$code);
	
				$code = str_replace ('[[vote]]',$info['vote'],$code);
				$className = 'bad';
				if ($info['vote'] >= 60){
					$className = 'medium';
				}
				if ($info['vote'] >= 75){
					$className = 'good';
				}
				$code = str_replace ('[[type]]',$className,$code);
	
				$titleClass= 'titleBig';
				if (strlen($info['title'])>15)
					$titleClass = 'titleMedium';
				if (strlen($info['title'])>24)
					$titleClass = 'titleSmall';
				$code = str_replace ('[[titleClass]]',$titleClass,$code);
	
			}
		}
		return $code;
	}
	
	private function __retrieveCover($sheetUrl){
		$cacheFileName = './covers/'.md5($sheetUrl).'.txt';
		if (file_exists($cacheFileName)){
			return file_get_contents($cacheFileName);
		}
		//preg_match ('/\/(.*?).html/',$sheetUrl,$fileName);
		//file_put_contents ('./test/'.$fileName[0].'.html',$content);
		//$content = file_get_contents ('./test/proto.html');
		$content = self::__urlGetContent($sheetUrl);
		preg_match ('/img([0-9]).gamespotcdn.(.*?)\/metacritic\/public\/www\/images\/products\/games\/([0-9])\/(.*?).jpg/',$content,$imageUrl);
		if (isset($imageUrl[0]))
			$finalUrl = 'http://'.$imageUrl[0];
		file_put_contents($cacheFileName,$finalUrl);
		return $finalUrl;
	}
	
}
